package com.nobody.facepk.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nobody.facepk.dao.CommentDao;
import com.nobody.facepk.model.Comment;
import com.nobody.facepk.model.User;
import com.nobody.facepk.sns.Event;
import com.nobody.facepk.sns.EventType;
import com.trg.dao.hibernate.GeneralDAO;

@Service
public class CommentService {
	private GeneralDAO generalDao;
	private CommentDao commentDao;
	private EventService   eventService;

	@Transactional(readOnly = false)
	public Comment getCommentInfo(Long id) {
		Comment comment = this.generalDao.find(Comment.class, id);
		return comment;
	}

	@Transactional(readOnly = false)
	public List getCommentByUserId(Long userId) {
		List<Comment> list = this.commentDao.getComment(userId);
		return list;
	}

	@Transactional(readOnly = false)
	public List getCommentByPicid(Long picId) {
		List<Comment> list = this.commentDao.getCommentByPic(picId);
		return list;
	}
	
	@Transactional(readOnly = false)
	public void insertComment(Long picId,Long refereId,String content,Long userId) {
		User user = this.generalDao.find(User.class, userId);
		Comment comment = new Comment();
		comment.setPictureId(picId);
		comment.setRefererId(refereId);
		comment.setUserId(userId);
		comment.setContent(content);
		this.generalDao.save(comment);
		//发起新鲜事
		Event event = new Event(userId,EventType.COMMENT,comment.getId());
		eventService.handleEvent(event);
	}
	
	
	
	@Autowired
	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	@Autowired
	public void setGeneralDao(GeneralDAO generalDao) {
		this.generalDao = generalDao;
	}
	@Autowired
	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}
}
